package com.wd.live.view.activity

import android.content.Intent
import android.widget.Toast
import com.blankj.utilcode.util.ToastUtils
import com.tencent.mm.opensdk.openapi.IWXAPI
import com.tencent.mm.opensdk.openapi.WXAPIFactory
import com.wd.live.databinding.ActivityMainBinding
import com.wd.live.model.base.BaseActivity
import com.wd.live.viewmodel.LoginViewModel
import com.wd.live.model.tool.BaseConstant
import com.wd.live.model.tool.LoginHelper
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

/**
 * 作者：马海钊
 * 时间：2023年8月17日14:58:39
 * 功能：微信登录页
 */
class MainActivity : BaseActivity<LoginViewModel, ActivityMainBinding>() {
    private var wxApi: IWXAPI? = null

    //    override fun onCreate(savedInstanceState: Bundle?) {
//        super.onCreate(savedInstanceState)
//
//    }
    override fun initData() {
        //初始化微信
        initWx()
        //协议
        binding.LoginDeal.setOnClickListener {
            startActivity(Intent(this, DealActivity::class.java))
        }

        if (LoginHelper.getPersonData(this) != null) {
            val intent = Intent(this, HomeActivity::class.java)
            startActivity(intent)
            finish()
        }

        binding.LoginButton.setOnClickListener {
            if (binding.LoginCheck.isChecked) {
                wxApi?.let { it1 -> viewModel.getWxCode(it1)}
            } else {
                Toast.makeText(this, "请先勾选协议", Toast.LENGTH_SHORT).show()
            }
        }

        viewModel.userInfo.observe(this) {
            Toast.makeText(applicationContext, "存储成功", Toast.LENGTH_SHORT).show()
            if (it?.userId!=null){
                LoginHelper.setPersonData(this,it)
                var intent=Intent(this,SexActivity::class.java)
                startActivity(intent)
                finish()
            }
        }

    }

    override fun getViewBinding() = ActivityMainBinding.inflate(layoutInflater)

    private fun initWx() {
        //初始化微信
        wxApi = WXAPIFactory.createWXAPI(this, BaseConstant.WX_APP_ID, false)
        wxApi?.registerApp(BaseConstant.WX_APP_ID)
    }

    override fun onResume() {
        super.onResume()
        LoginHelper.getWxLoginCode(this)?.let {
            viewModel.doLogin(it)
            Toast.makeText(applicationContext, "code:"+it, Toast.LENGTH_SHORT).show()
        }
    }
}